<template>
    <view class="img-cell">
        <view class="">
            <view class="add-img" v-if="imageList.length < num && look == 0" @tap="chooseImage()">
                <uni-icons type="camera" class="iconColor" size="80"></uni-icons>
                <text style="color: #cccccc">{{ text }}</text>
            </view>
        </view>
        <block v-for="(image, oneimageindex) in imageList" :key="oneimageindex">
            <view class="img-item">
                <image
                    class="img"
                    :src="easyGetImgUrl(image)"
                    :data-src="easyGetImgUrl(image)"
                    @tap="previewImage"
                    mode="aspectFill"></image>
                <view @click="del(oneimageindex)" v-if="look == 0">
                    <uni-icons class="btn-del" type="closeempty" customIcons="icon-white"></uni-icons>
                </view>
            </view>
        </block>
		<privacy-agreement></privacy-agreement>
    </view>
</template>

<script>
import permision from "@/utils/permission.js"
import { uploadImage } from "@/service/api/file.js"
import privacyAgreement from "@/components/privacy-agreement/privacy-agreement.vue"
export default {
    props: {
        data: {
            // 数据
            type: String,
            default: "",
        },
        index: {
            type: Number,
            default: 0,
        },
        look: {
            type: String,
            default: "0",
        },
        num: {
            type: Number,
            default: 0,
        },
        date: {
            type: String,
            default: "",
        },
        text: {
            type: String,
            default: "添加图片",
        },
    },
	components:{
		privacyAgreement
	},
    data() {
        return {
            imageList: [],
        }
    },
    mounted() {
        if (this.data) {
            if (this.num == 1) {
                this.imageList = this.data.split()
            } else {
                if (this.data.indexOf(",") != -1) {
                    this.imageList = this.data.split(",")
                } else {
                    this.imageList = this.data.split()
                }
            }
        }
    },
    methods: {
        ed(e) {
            //选择
            this.showPicker = false
            if (e) {
                this[this.type] = e.value
                this.$emit("ed", this.index, e.value)
            }
        },
        //选择图片
		//#ifdef APP
		async chooseImage() {
			let that = this
			const result = await permision.premissionCheck("EXTERNAL_STORAGE")
			if(result == 1) {  
			    // 此处处理各自业务 如：uni.chooseImage  
				if (that.look == 0) {
					let select = 9
					if (that.num == 1) {
						select = 1
					} else {
						select = that.num - that.imageList.length
					}
					uni.chooseImage({
						count: select, //默认9
						sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ["album"], //从相册选择
						success: function (res) {
							that.upLoadPicFn(res)
						},
					})
				}
			}
		},
		//#endif
		// #ifndef APP
        chooseImage() {
            if (this.look == 0) {
                let that = this
                let select = 9
                if (this.num == 1) {
                    select = 1
                } else {
                    select = this.num - this.imageList.length
                }
                uni.chooseImage({
                    count: select, //默认9
                    sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
                    sourceType: ["album"], //从相册选择
                    success: function (res) {
                        that.upLoadPicFn(res)
                    },
                })
            }
        },
		// #endif
        //删除图片
        del(index) {
            this.imageList.splice(index, 1)
            this.$emit("chooseImage", this.index, this.imageList.join(","))
        },
        //上传图片
        upLoadPicFn(files) {
            for (var i = 0; i < files.tempFilePaths.length; i++) {
                let param = {
                    filePath: files.tempFilePaths[i],
                    formData: {},
                }
                uploadImage(param).then((e) => {
                    if (e.Data) {
                        this.imageList.push(e.Data.FullPath)
                        if (this.imageList.length > 9) {
                            this.imageList.length = 9
                        }
                        this.$emit("chooseImage", this.index, this.imageList.join(","))
                    }
                })
            }
        },
        //预览图片
        previewImage(e) {
            let looklist = []
            this.imageList.forEach((e, i, arr) => {
                arr[i] = this.easyGetImgUrl(e)
            })
            var current = e.target.dataset.src
            uni.previewImage({
                current: current,
                urls: this.imageList,
            })
        },
    },
}
</script>

<style lang="scss">
.checkmain {
    display: flex;
    flex-wrap: wrap;

    label {
        display: flex;
        width: 50%;

        view {
            white-space: nowrap;
        }
    }
}
.iconColor {
    color: #cccccc;
}
</style>
